﻿{% extends "base.html" %}
{% block head %}

	options_add={
		buttons:{"提交":function(){
							var id = $('#txt_departmentID').val();
							var name = $("#txt_departmentName").val();
							$.ajax({
								url:"/addDepartment/",
								data:"id="+id+"&name="+name+"&type=add",
								method:'get',
								success:function(data){
											if(data == "success"){
												alert("更新成功！");$("#addDepartment").dialog("close");window.location.reload();
											}else{
												alert(data);
											}
										},
								error:function(error){alert(error);}
							})
							},
					 "关闭":function(){$(this).dialog("close")}},
			width:500,
			title:"添加部门",
	}
	options_update={
		buttons:{"提交":function(){
							var id = $('#txt_departmentID').val();
							var name = $("#txt_departmentName").val();
							$.ajax({
								url:"/addDepartment/",
								data:"id="+id+"&name="+name+"&type=update",
								method:'get',
								success:function(data){
											if(data == "success"){
												alert("更新成功！");$("#addDepartment").dialog("close");window.location.reload();
											}else{
												alert(data);
											}
										},
								error:function(error){alert(error);}
							})
							},
					 "关闭":function(){$(this).dialog("close")}},
			width:500,
			title:"添加部门",
	}
    $(function(){
		$("#addDepartment").css("display","none");
		$("#btn_add").bind('click',fn_add)
	})
	
	/*
	* 添加部门
	*/
	function fn_add(){
		$("#txt_departmentID").val("");
		$("#txt_departmentName").val("");
		$("#addDepartment").css("display","block").dialog(options_add);
	}
	/*
	* 修改部门
	*/
	function fn_update(departID,departName){
		$("#txt_departmentID").val(departID);
		$("#txt_departmentName").val(departName);
		$("#addDepartment").css("display","block").dialog(options_update);
	}
	/*
	* 删除部门
	*/
	function fn_del(departID){
		$.ajax({
			url:"/delDepartment/",
			data:"id="+departID,
			method:"get",
			success:function(data){
						if(data == "success"){
							alert("删除成功！");
							window.location.reload();
						}
						else if(data=="fail"){
							alert("删除失败！");
						}
						else{
							alert("部门ID异常！");
						}
					},
			error:function(data){alert(data)}
		})
	}
</script>
{% endblock %}
{% block content %}

    {% csrf_token %}
	<button type="button" class="btn btn-primary" style="float:right;" id="btn_add" >添&nbsp; &nbsp;加</button>
	<div style="margin-bottom:2px;"></div>
    <table class="table table-striped">
    	<tr>
    		<td>部门ID</td>
    		<td>部门名称</td>
    		<td>修改</td>
    		<td>删除</td>
    	</tr>
		{% for item in result %}
    	<tr>
    		<td>{{item.DepartmentID}}</td>
    		<td>{{item.DepartmentName}}</td>
			<td><a onclick="fn_update('{{ item.DepartmentID}}','{{item.DepartmentName}}')">修改</a></td>
			<td><a onclick="fn_del('{{item.DepartmentID}}')">删除</a></td>
    	</tr>
		{% endfor %}
    </table>
	<div id="addDepartment" class="form-horizontal">
		<div class="form-group">
			<label for="inputEmail3" class="col-sm-3 control-label">部门ID</label>
			<div class="col-sm-8">
				<input type="email" class="form-control" id="txt_departmentID" placeholder="部门ID">
			</div>
		</div>
		<div class="form-group">
			<label for="inputEmail3" class="col-sm-3 control-label">部门名称</label>
			<div class="col-sm-8">
				<input type="email" class="form-control" id="txt_departmentName" placeholder="部门名称">
			</div>
		</div>
	</div>
</form>
{% endblock %}